package com.itboye.sunsun.db;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.TieZiCaoGaoBean;
import com.itboye.sunsun.choosecity.CityModel;
import com.itboye.sunsunhome.R;
/**
 * 保存草稿
 * @author admin
 *
 */
public class DBManager {

	DBHelper mDbHelper;
	RiDBHellp dbHellp;
	SQLiteDatabase db;
	Context mContext;
	private RecordSQLiteOpenHelper helper = new RecordSQLiteOpenHelper(App.ctx);;
	public static final String DB_NAME = "china_city_name.db";
	public static final String DB_PATH = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/" + "com.itboye.sunsunhome" ; // 存放路径
	private final int BUFFER_SIZE = 400000;
	public DBManager(Context mContext) {
		this.mContext=mContext;
		mDbHelper = new DBHelper(mContext);
		dbHellp=new RiDBHellp(mContext);
	}

	/***
	 * 添加进草稿
	 * @param uid
	 * @param title
	 * @param content
	 * @param time
	 * @param re
	 * @param img
	 * @return
	 */
	public long addNote(String uid,String title, String content,String img) {
		db = mDbHelper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("uid", uid);
		cv.put("title", title);
		cv.put("content", content);
		cv.put("img", img);
		long num = db.insert("mynote", null, cv);
		db.close();
		return num;
	}
	

	
	/***
	 * 添加进草稿
	 * @param uid
	 * @param title
	 * @param content
	 * @param time
	 * @param re
	 * @param img
	 * @return
	 */
	public long addRich(String uid,String title, String content,String imgid) {
		db = mDbHelper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("uid", uid);
		cv.put("title", title);
		cv.put("content", content);
		cv.put("imgid", imgid);
//		cv.put("img", img);
		long num = db.insert("mynotee", null, cv);
		db.close();
		return num;
	}
	

	/**
	 * 
	 * 
	 * @return
	 */
	public ArrayList<TieZiCaoGaoBean> queryNote(String uid) {
		ArrayList<TieZiCaoGaoBean> list = new ArrayList<TieZiCaoGaoBean>();
		db = mDbHelper.getReadableDatabase();

		Cursor mCursor = db.query("mynotee", null, "uid=?", new String[]{uid}, null, null, null);
		if (mCursor != null) {
			while (mCursor.moveToNext()) {
				TieZiCaoGaoBean note = new TieZiCaoGaoBean();
				String title = mCursor.getString(mCursor
						.getColumnIndex("title"));
				note.setTitle(title);
				String content = mCursor.getString(mCursor
						.getColumnIndex("content"));
				String img = mCursor.getString(mCursor
						.getColumnIndex("imgid"));
				note.setId(uid);
				note.setTitle(title);
				note.setImg(img);
				note.setContent(content);
				list.add(note);
			}
			mCursor.close();
		}
		db.close();
		return list;
	}
	/**
	 * 删除缓存
	 * @param id
	 * @param title
	 * @param content
	 * @return
	 */
	public long deleteRi(String uid) {
		db = mDbHelper.getWritableDatabase();
		long result=db.delete("mynotee", "uid=?", new String[]{uid});
		return result;
	}

	/**
	 * 删除缓存
	 * @param id
	 * @param title
	 * @param content
	 * @return
	 */
	public long delete(String uid) {
		db = mDbHelper.getWritableDatabase();
		long resultdb=db.delete("mynote", "uid=?", new String[]{uid});
		return resultdb;
	}
	
	/**
	 * 被调用方法
	 */
	public void openDateBase()
	{
		this.db = this.openDateBase(DB_PATH + "/" + DB_NAME);

	}
	/**
	 * 被调用方法
	 */
	public void closeDateBase()
	{
		if(this.db!=null)
		this.db.close();

	}
	
	
	/**
	 * 打开数据库
	 * 
	 * @param dbFile
	 * @return SQLiteDatabase
	 * @author sy
	 */
	private SQLiteDatabase openDateBase(String dbFile){
		File file = new File(dbFile);
		if (!file.exists())
		{
			// // 打开raw中得数据库文件，获得stream流
			InputStream stream = mContext.getResources().openRawResource(R.raw.china_city_name);
			try
			{

				// 将获取到的stream 流写入道data中
				FileOutputStream outputStream = new FileOutputStream(dbFile);
				byte[] buffer = new byte[BUFFER_SIZE];
				int count = 0;
				while ((count = stream.read(buffer)) > 0)
				{
					outputStream.write(buffer, 0, count);
				}
				outputStream.close();
				stream.close();
				SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
				return db;
			} catch (FileNotFoundException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return db;
	}
	public  String queryCityNo(String cityName){
//		db = mDbHelper.getReadableDatabase();
		Cursor	cursor=null;
		if(this.db!=null)
			cursor = db.query("hat_city", new String[]{"cityID"}, "city=?", new String[]{cityName}, null	,null, null);
		String cityCode = null;
		if (cursor != null)
			while(cursor.moveToNext()){
				cityCode = cursor.getString(0);
			}
		cursor.close();
		db.close();
		return cityCode;
	}
	/***
	 * 查询区域列表
	 * @param cityName
	 * @return
	 */
	public  ArrayList<CityModel> queryArea(String cityNo){

		Cursor	cursor=null;
		ArrayList<CityModel> arrayArea=new ArrayList<>();
		if(this.db!=null)
//			cursor=db.rawQuery(", selectionArgs)
			cursor = db.query("hat_area", null, "father like ?", new String[]{cityNo+"%"}, null	,null, null);
		if (cursor != null)
			while(cursor.moveToNext()){
				CityModel cityModel=new CityModel();
				cityModel.setAreaCode(cursor.getString(cursor.getColumnIndexOrThrow("areaID")));
				cityModel.setCityCode(cursor.getString(cursor.getColumnIndexOrThrow("father")));
				cityModel.setAreaName(cursor.getString(cursor.getColumnIndexOrThrow("area")));
				arrayArea.add(cityModel);
			}
		cursor.close();
		db.close();
		return arrayArea;
	}

	public String queryPriovinceNo(String city) {
		// TODO Auto-generated method stub

		db = mDbHelper.getReadableDatabase();
		Cursor	cursor=null;
		if(this.db!=null)
			cursor = db.query("hat_province", new String[]{"provinceID"}, "province=?", new String[]{city}, null	,null, null);
		String cityCode = null;
		if (cursor != null)
			while(cursor.moveToNext()){
				cityCode = cursor.getString(0);
			}
		cursor.close();
		db.close();
		return cityCode;
	}

	/**
	 * 清空数据
	 */
	public void deleteData() {
		db = helper.getWritableDatabase();
		db.execSQL("delete from records");
		db.close();
	}
}
